package com.bk;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;


import com.mj.printer.BinaryTreeInfo;

@SuppressWarnings("unchecked")
public class BinarySearchTree<E> implements Tree<E>, BinaryTreeInfo {
	private int size;
	private Node<E> root;
	private Comparator<E> comparator;

	public BinarySearchTree() {
		this(null);
	}

	public BinarySearchTree(Comparator<E> comparator) {
		this.comparator = comparator;
	}

	@Override
	public int size() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return this.size == 0;
	}

	@Override
	public void clear() {

	}

	/*
	 * 前序遍历
	 */
//	public void preorderTraversalPrint() {
//		preorderTraversal(root);
//	}
//
//	private void preorderTraversal(Node<E> node) {
//		if (node == null) {
//			return;
//		}
//		System.out.println(node.element);
//		preorderTraversal(node.left);
//		preorderTraversal(node.right);
//	}

	public void preorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		preorder(root, visitor);
	}

	private void preorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
		preorder(node.left, visitor);
		preorder(node.right, visitor);
	}

	public void preorderTraversalIterate(Visitor<E> visitor) {
		Stack<Node<E>> stack = new Stack<>();
		stack.push(root);
		while (!stack.isEmpty()) {
			Node<E> newNode = stack.pop();
			visitor.visit(newNode.element);
			if (newNode.right != null) {
				stack.push(newNode.right);
			}
			if (newNode.left != null) {
				stack.push(newNode.left);
			}
		}
	}

	/*
	 * 中序遍历
	 * 
	 */
//	public void inorderTraversalPrint() {
//		inorderTraversal(root);
//	}
//
//	private void inorderTraversal(Node<E> node) {
//		if (node == null) {
//			return;
//		}
//		inorderTraversal(node.left);
//		System.out.println(node.element);
//		inorderTraversal(node.right);
//	}

	public void inorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		inorder(root, visitor);
	}

	private void inorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		inorder(node.left, visitor);
		if (visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
		inorder(node.right, visitor);
	}

	public void inorderIterate(Visitor<E> visitor) {
		if (visitor.stop) {
			return;
		}
		Queue<E> queue = new LinkedList<>();
		Stack<Node<E>> stack = new Stack<>();
		Node<E> node = root;
		while (!stack.isEmpty() || node != null) {
			if (node != null) {
				stack.push(node);
				node = node.left;
			} else {
				node = stack.pop();
				queue.offer(node.element);
				node = node.right;
			}
		}

	}

//		Queue<Node<E>> queue = new LinkedList<E>();
//		queue
//				

	/*
	 * 后序遍历
	 * 
	 */
//	public void postorderTraversalPrint() {
//		postorderTraversal(root);
//	}
//
//	private void postorderTraversal(Node<E> node) {
//		if (node == null) {
//			return;
//		}
//		postorderTraversal(node.left);
//		postorderTraversal(node.right);
//		System.out.println(node.element);
//	}

	public void postorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		postorder(root, visitor);
	}

	private void postorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		postorder(node.left, visitor);
		postorder(node.right, visitor);
		if (visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
	}

	/*
	 * 层序遍历
	 * 
	 */
//	public void levelorderTraversalPrint() {
//		levelorderTraversal(root);
//	}
//
//	private void levelorderTraversal(Node<E> node) {
//		if (node == null) {
//			return;
//		}
//		Queue<Node<E>> queue = new LinkedList<>();
//		queue.offer(node);
//		while (queue.size() > 0) {
//			Node<E> node2 = queue.poll();
//
//			System.out.println(node2.element);
//			if (node2.left != null) {
//				queue.offer(node2.left);
//			}
//			if (node2.right != null) {
//				queue.offer(node2.right);
//			}
//		}
//	}

	public void levelorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		levelorder(root, visitor);
	}

	public void levelorder(Node<E> node, Visitor<E> visitor) {
		if (node == null) {
			return;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(node);
		while (queue.size() > 0) {
			Node<E> node2 = queue.poll();
			if (visitor.stop) {
				return;
			}
			visitor.stop = visitor.visit(node2.element);
			if (node2.left != null) {
				queue.offer(node2.left);
			}
			if (node2.right != null) {
				queue.offer(node2.right);
			}
		}
	}

	public boolean isComplete(Visitor<E> visitor) {
		if (root == null) {
			return false;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		boolean isLeaf = false;
		while (queue.size() > 0) {
			Node<E> node = queue.poll();
			if (isLeaf && node.isLeaf()) {
				return false;
			}
			if (node.left != null) {
				queue.offer(node.left);
			} else {
				if (node.right != null) {
					return false;
				}
			}

			if (node.right != null) {
				queue.offer(node.right);
			} else {
				if (node.left == null) {
					isLeaf = true;
				}
			}

		}
		return true;
	}

//有问题
//	public boolean isComplete1(Visitor<E> visitor) {
//		if (root == null || (root.left == null && root.right != null)) {
//			return false;
//		}
//		Queue<Node<E>> queue = new LinkedList<>();
//		queue.offer(root);
//		boolean isExistHalf = false;
//		while (queue.size() != 0) {
//			Node<E> node = queue.poll();
//			Node<E> left = node.left;
//			Node<E> right = node.right;
//			if (isExistHalf && !node.isLeaf()) {
//				return false;
//			}
//
//			if (node.hasTwoChildren()) {
//				queue.offer(left);
//				queue.offer(right);
//			} else if (left == null && right != null) {
//				return false;
//			} else {
//				if (left != null) {
//					queue.offer(left);
//				}
//				isExistHalf = true;
//			}
//		}
//
//		return true;
//	}

	public int height() {
		return height(root);
	}

	private int height(Node<E> node) {
		if (node == null) {
			return 0;
		}
		return 1 + Math.max(height(node.left), height(node.right));
	}

	public int height2() {
		return caculateHeight(root);
	}

	private int caculateHeight(Node<E> node) {
		if (node == null) {
			return 0;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(node);
		int levelSize = 1;
		int height = 0;
		while (queue.size() > 0) {
			Node<E> node2 = queue.poll();
			levelSize--;
			if (node2.left != null) {
				queue.offer(node2.left);
			}
			if (node2.right != null) {
				queue.offer(node2.right);
			}
			if (levelSize == 0) {
				levelSize = queue.size();
				height++;
			}
		}
		return height;
	}

	public int widthOfBinaryTree() {
		Node<Integer> node = (Node<Integer>) this.root();
		if (node == null) {
			return 0;
		}
		int size = 1;
		node.element = 1;
		int diff = 0;
		Node<Integer> firNode = null;
		Node<Integer> lastNode = null;
		Queue<Node<Integer>> queue = new LinkedList<Node<Integer>>();
		queue.add(node);
		while (!queue.isEmpty()) {
			boolean isFirst = true;
			while (size != 0) {
				size--;
				Node<Integer> newnode = queue.poll();
				if (isFirst) {
					firNode = newnode;
					isFirst = false;
				}
				if (size == 0) {
					lastNode = newnode;
				}
				if (newnode.left != null) {
					newnode.left.element = newnode.element * 2;
					queue.add(newnode.left);
				}
				if (newnode.right != null) {
					newnode.right.element = newnode.element * 2 + 1;
					queue.add(newnode.right);
				}
			}
			size = queue.size();
			diff = Math.max(diff, lastNode.element - firNode.element + 1);
		}
		return diff;
	}

	private Node<E> predecessor(Node<E> node) {
		if (node == null) {
			return null;
		}
		if (node.left != null) {
			Node<E> newNode = node.left;
			while (newNode.right != null) {
				newNode = newNode.right;
			}
			return newNode;
		}

		while (node.parent != null && node == node.parent.left) {
			node = node.parent;
		}
		return node.parent;

	}

	private Node<E> successor(Node<E> node) {
		if (node == null) {
			return node;
		}

		if (node.right != null) {
			Node<E> newNode = node.right;
			while (newNode.left != null) {
				newNode = newNode.left;
			}
			return newNode;
		}
		if (node.right == null && node.parent != null) {
			while (node.parent != null && node == node.parent.right) {
				node = node.parent;
			}
			return node.parent;
		}
		return node;
	}

	public void remove(E element) {
		remove(node(element));
	}

	public boolean contains(E element) {
		return false;
	}

	public void remove(Node<E> node) {
		if (node == null) {
			return;
		}
		size--;
		if (node.hasTwoChildren()) {
			Node<E> preNode = this.predecessor(node);
			node.element = preNode.element;// 把需要被删除的节点的element换成他的前驱节点的element，这时候只需要删除前驱节点不就完了吗？前驱节点是度为0/1的；
			node = preNode;// node指向前驱节点，后面删除node就等于删除前驱节点；现在node就是一个度为0或者1的节点，然后接下来执行删除度为0或者1的节点；
		}

		Node<E> replaceNode = node.left != null ? node.left : node.right;// 将要替换node的节点
		if (replaceNode != null) {// 替换的节点不为空 度为1
			replaceNode.parent = node.parent;
			if (node.parent == null) {
				root = replaceNode;
			} else {
				if (node.parent.left != null) {
					node.parent.left = replaceNode;
				} else {
					node.parent.right = replaceNode;
				}
			}
		} else if (node.parent == null) {// 替换的节点为空，并且父节点为空，只有他一个节点 度为0
			root = null;
		} else {// 替换的节点为空 度为0
			if (node.parent.left == node) {
				node.parent.left = null;
			} else {
				node.parent.right = null;
			}
//			node = null;
		}

	}

	private Node<E> node(E element) {
		return null;
	}

	@Override
	public void add(E element) {
		elementNotNullCheck(element);
		if (this.root == null) {
			root = new Node<E>(element, null);
			size++;
			return;
		}
		Node<E> parent = null;
		Node<E> node = root;
		int cmp = 0;
		while (node != null) {
			parent = node;
			cmp = compare(element, node.element);
			if (cmp > 0) {
				node = node.right;
			} else if (cmp < 0) {
				node = node.left;
			} else {
				node.element = element;
				return;
			}
		}
		Node<E> newNode = new Node<E>(element, parent);
		if (cmp > 0) {
			parent.right = newNode;
		} else {
			parent.left = newNode;
		}
		size++;
	}

	private int compare(E element1, E element2) {
		if (this.comparator != null) {
			return comparator.compare(element1, element2);
		}
		return ((Comparable<E>) element1).compareTo(element2);
	}

	public void elementNotNullCheck(E element) {
		if (element == null) {
			throw new IllegalArgumentException("element not be null");
		}
	}

	public static abstract class Visitor<E> {
		boolean stop;

		abstract boolean visit(E element);
	}

	@SuppressWarnings("unused")
	private static class Node<E> {
		E element;
		Node<E> left;
		Node<E> right;
		Node<E> parent;

		public Node(E element, Node<E> parent) {
			this.element = element;
			this.parent = parent;
		}

		public boolean isLeaf() {
			return left == null && right == null;
		}

		public boolean hasTwoChildren() {
			return left != null && right != null;
		}
	}

	@Override
	public Object root() {
		return this.root;
	}

	@Override
	public Object left(Object node) {
		return ((Node<E>) node).left;
	}

	@Override
	public Object right(Object node) {
		return ((Node<E>) node).right;
	}

	@Override
	public Object string(Object node) {
		return ((Node<E>) node).element;
	}

	public void invertTree() {
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(this.root);

		while (queue.size() > 0) {
			Node<E> node = queue.poll();
			Node<E> lefTreeNode = node.left;
			Node<E> righTreeNode = node.right;

			if (lefTreeNode != null) {
				queue.offer(lefTreeNode);
				node.right = lefTreeNode;
			} else {
				node.right = null;
			}

			if (righTreeNode != null) {
				queue.offer(righTreeNode);
				node.left = righTreeNode;
			} else {
				node.left = null;
			}

		}

	}
}
